package com.cuz.daileetcode;

import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import javax.xml.stream.FactoryConfigurationError;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.List;

/**
 * @author cuzz
 * @version 1.0
 * @description: 2. 两数相加
 * 给你两个 非空 的链表，表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的，并且每个节点只能存储 一位 数字。
 * <p>
 * 请你将两个数相加，并以相同形式返回一个表示和的链表。
 * <p>
 * 你可以假设除了数字 0 之外，这两个数都不会以 0 开头。
 * <p>
 * <p>
 * <p>
 * 示例 1：
 * <p>
 * <p>
 * 输入：l1 = [2,4,3], l2 = [5,6,4]
 * 输出：[7,0,8]
 * 解释：342 + 465 = 807.
 * 示例 2：
 * <p>
 * 输入：l1 = [0], l2 = [0]
 * 输出：[0]
 * 示例 3：
 * <p>
 * 输入：l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
 * 输出：[8,9,9,9,0,0,0,1]
 * <p>
 * <p>
 * 提示：
 * <p>
 * 每个链表中的节点数在范围 [1, 100] 内
 * 0 <= Node.val <= 9
 * 题目数据保证列表表示的数字不含前导零
 * @date 13:40 2021/9/21
 **/
public class Day4 {
    private static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public static class S1 {

        public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode head = null, tail = null;
            int carry = 0;
            while (l1 != null || l2 != null) {
                int n1 = l1 != null ? l1.val : 0;
                int n2 = l2 != null ? l2.val : 0;
                int sum = n1 + n2 + carry;
                if (head == null) {
                    head = tail = new ListNode(sum % 10);
                } else {
                    tail.next = new ListNode(sum % 10);
                    tail = tail.next;
                }
                carry = sum / 10;
                if (l1 != null) {
                    l1 = l1.next;
                }
                if (l2 != null) {
                    l2 = l2.next;
                }
            }
            if (carry > 0) {
                tail.next = new ListNode(carry);
            }
            return head;
        }
    }

    public static class S2 {
        public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode res = new ListNode(0);
            if (l1 == null || l2 == null) {
                return res;
            }
            ListNode curIndexL1 = l1;
            ListNode curIndexL2 = l2;
            ListNode curIndexRes = res;
            int needCarry = 0;
            while (curIndexL1 != null && curIndexL2 != null) {
                int tempSum = curIndexL1.val + curIndexL2.val;
                tempSum += needCarry;
                needCarry = tempSum / 10;
                curIndexRes.next = new ListNode(tempSum - needCarry * 10);
                curIndexL1 = curIndexL1.next;
                curIndexL2 = curIndexL2.next;
                curIndexRes = curIndexRes.next;
            }
            if (curIndexL1 == null && curIndexL2 == null) {
                if (needCarry!=0){
                    curIndexRes.next=new ListNode(needCarry);
                }
                return res.next;
            }
            if (curIndexL1 == null) {
                curIndexRes.next = curIndexL2;
            } else {
                curIndexRes.next = curIndexL1;
            }
            curIndexRes = curIndexRes.next;
            while (needCarry != 0) {
                int tempValue = curIndexRes.val + needCarry;
                needCarry = tempValue / 10;
                curIndexRes.val = tempValue - tempValue / 10 * 10;
                if (curIndexRes.next == null&&needCarry!=0) {
                    curIndexRes.next = new ListNode(needCarry);
                    break;
                }
                curIndexRes = curIndexRes.next;
            }
            return res.next;
        }

        public static void sout(ListNode l1) {
            while (l1 != null) {
                System.out.print("  ");
                System.out.print(l1.val);
                l1=l1.next;
            }
            System.out.println();
        }

        public static void main(String[] args) {
            //[2,4,3]
            //[5,6,4]
            //[9,9,9,9,9,9,9]
            //[9,9,9,9]
            List<Integer> l1 = Arrays.asList(9, 9, 9, 9, 9, 9, 9);
            List<Integer> l2 = Arrays.asList(9, 9, 9, 9);
            ListNode listNode1 = new ListNode(9);
            ListNode listNode2 = new ListNode(9);
            ListNode listNode3 = new ListNode(9);
            ListNode listNode4 = new ListNode(9);
            ListNode listNode5 = new ListNode(9);
            ListNode listNode6 = new ListNode(9);
            ListNode listNode7 = new ListNode(9);
            listNode1.next = listNode2;
            listNode2.next = listNode3;
            listNode3.next = listNode4;
            listNode4.next = listNode5;
            listNode5.next = listNode6;
            listNode6.next = listNode7;

            ListNode listNode11 = new ListNode(9);
            ListNode listNode12 = new ListNode(9);
            ListNode listNode13 = new ListNode(9);
            ListNode listNode14 = new ListNode(9);
            listNode11.next = listNode12;
            listNode12.next = listNode13;
            listNode13.next = listNode14;
            ListNode listNode = addTwoNumbers(listNode1, listNode11);
            sout(listNode);

        }
    }
}
